package com.xq.sboot.amqp.tut1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 功能说明:消息发送者类.
 * <p>创建时间: 2019-01-02 16:57</p>
 *
 * @author 郭凯强
 * @version 1.0-SNAPSHOT
 */
public class Sender {
    private final Logger logger = LoggerFactory.getLogger(Sender.class);
    private final Marker marker = MarkerFactory.getMarker("Sender");

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private Queue queue;

    /**
     * 发送消息的方法
     */
    @Scheduled(fixedDelay = 5000, initialDelay = 500)
    public void send() {
        String message = "Hello World!";
        //routingKey=this.queue.getName, message=message
        this.rabbitTemplate.convertAndSend(this.queue.getName(), message);
        logger.debug(marker, "{} Sent {}", "[x]", message);
    }
}
